import json


def create_add_template(schema_name, table_name, id_field):
    table_name = table_name.upper()
    template = {table_name: {"&": "$$"}}
    if id_field is not None:
        template[table_name]["id"] = "${%s}" % id_field

    if schema_name is not None:
        template[table_name]["@schema"] = schema_name

    return template


def create_del_template(schema_name, table_name, id_field):
    table_name = table_name.upper()
    template = {table_name: {}}
    if id_field is not None:
        template[table_name]["id"] = "${%s}" % id_field
    if schema_name is not None:
        template[table_name]["@schema"] = schema_name

    return template


def create_edit_template(schema_name, table_name, id_field):
    table_name = table_name.upper()
    # 修改
    template = {table_name: {"&": "$$"}}
    if id_field is not None:
        template[table_name]["id"] = "${%s}" % id_field
    if schema_name is not None:
        template[table_name]["@schema"] = schema_name
    return template


def create_list_template(schema_name, table_name):
    table_name = table_name.upper()
    # 列表
    template = {
        "[]": {
            table_name: {
                "&": "$$"
            },
            "perPage": "${perPage}",
            "page": "${page}",
            "query": 2,
        },
        "total@": "/[]/total",
    }

    if schema_name is not None:
        template["[]"][table_name]["@schema"] = schema_name

    return template


def create_row_template(schema_name, table_name, id_field):
    table_name = table_name.upper()
    # 详情
    template = {table_name: {}}
    if id_field is not None:
        template[table_name]["id"] = "${%s}" % id_field
    if schema_name is not None:
        template[table_name]["@schema"] = schema_name

    return template


def create_select_template(schema_name, table_name):
    table_name = table_name.upper()
    template = {
        "[]": {
            table_name: {
                "&": "$$",
                "@column": "column_1:label,column_2:value,column_3,column_4",
                "@order": "column_1-",
                "@group": "column_1,column_2",
            },
            "page": "${page}",
            "perPage": "${perPage}",
            "query": 2,
        },
        "total@": "/[]/total",
    }
    if schema_name is not None:
        template["[]"][table_name]["@schema"] = schema_name

    return template


def create_tree_template(schema_name, table_name):
    table_name = table_name.upper()
    template = {
        "[]": {
            table_name: {
                "&": "$$",
                "@column": "id:value,parent_id:parentId,organ_name:label,organ_code:weight",
            },
            "page": "${page}",
            "perPage": "${perPage}",
            "query": 2,
        },
        "total@": "/[]/total",
    }
    if schema_name is not None:
        template["[]"][table_name]["@schema"] = schema_name

    return template


def create_get_template():
    template = {
        "[]": {
            "join": "&/TABLE_2/id@",
            "TABLE_1": {
                "column_1": "${column_1}",
                "column_2": "${column_2}",
                "@like": "column_1,column_2",
            },
            "TABLE_2": {
                "id@": "/TABLE_1/TABLE_2_id",
                "column_3": "${column_3}",
                "column_4": "${column_4}",
                "@column": "column_3,column_4",
                "@like": "column_3,column_4",
            },
            "page": "${page}",
            "perPage": "${perPage}",
            "query": 2,
        },
        "total@": "/[]/total",
    }

    return template


if __name__ == "__main__":
    import logging

    fmt = logging.Formatter("%(asctime)s|%(levelname)s|%(filename)s|%(lineno)s|%(message)s")

    sh = logging.StreamHandler()
    sh.setFormatter(fmt)
    sh.setLevel(logging.INFO)

    logger = logging.getLogger("root")
    # logger.handlers = []
    logger.addHandler(sh)
    logger.setLevel("INFO")

    logging.info(json.dumps(create_get_template(), ensure_ascii=False))
